<template>
	<view class="person">
		<view class="content">
			<!-- 头像显示 -->
			<view class="touxian">
				<image @click="chooseImages" :src="head_url" mode="scaleToFill" style="width: 100rpx; height: 100rpx;"></image>
			</view>
			<!-- 填写信息内容 -->
			<view class="form">
				<view class="userName">
					<label>姓名</label>
					<input type="text" v-model="userinfo.user_name" placeholder="姓名" :disabled="bunengai">
				</view>
				<view class="region">
					<label>性别</label>
					<view @click="males">
						<text :class="userinfo.user_sex == 1 ? 'active': ''"></text><text>男</text>
					</view>
					<view @click="females">
						<text :class="userinfo.user_sex == 2 ? 'active': ''"></text><text>女</text>
					</view>
				</view>

				<view class="title">
					<label>职称</label>
					<input type="text" v-model="userinfo.holder" placeholder="请输入" :disabled="bunengai">
				</view>
				<view class="education">
					<label>学历</label>
					<picker class="pickers" @change="bindPickerChange" :value="educationIndex" :range="educationArr" :disabled="bunengai">
						<view class="uni-input viewInput">{{educationArr[educationIndex]}}</view>
						<text class="iconfont icon-xiajiantou icons"></text>
					</picker>

				</view>
				<view class="university">
					<label>毕业院校</label>
					<input type="text" v-model="userinfo.school_title" placeholder="请输入" :disabled="bunengai">
				</view>
				<view class="birth">
					<label>出生年月</label>
					<view :disabled="bunengai" @tap="show"><text class="qingshuru">{{days}}</text> <text class="iconfont icon-xiajiantou icon-xiajiantouColor"></text></view>
				</view>

				<view class="clinicmaingraph">
					<view>
						<text>职业证书(一张)</text>
						<text @click="clinicmaingraph"><text>添加</text>+</text>
					</view>
					<view v-if="creden" class="images">
						<image v-for="(item,index) in creden" :key="item.id" :src="item" mode="scaleToFill" style="width: 100rpx; height: 100rpx;"></image>
					</view>
				</view>

				<view class="personal">
					<label>个人介绍</label>
				</view>
				<input id="clinicProfile" type="text" v-model="userinfo.referral" :disabled="bunengai" />

				<view class="addPicture">
					<view>
						<text>添加图片(最多四张)</text>
					</view>
					<view class="imagess">
						<image v-for="(item,index) in addPicture" :key="item.id" :src="item" mode="scaleToFill" style="width: 150rpx; height: 150rpx;"></image>
						<view @click="addPictures">
							<text class="jiahao">+</text>
						</view>
					</view>
				</view>

			</view>

			<!-- 点击按钮 -->
			<view v-if="detailsNum == 0" class="joinDetails" @click="detas">
				<text>编辑</text>
			</view>

			<!-- 底部双按钮 -->
			<view v-if="detailsNum == 1" class="dibusuananniu">
				<text @click="cancel">取消编辑</text>
				<text @click="examine">编辑完成</text>
			</view>

			<!-- 时间选择器 -->
			<e-picker-plus :defaultValue="defaultValues" mode="YMD" startYear="1920" ref="picker" @confirm="confirms" />
		</view>
	</view>
</template>
<script>
	export default {
		onLoad() {
			// 获取oos上传
			this.uploadOosMethos();
			this.userinfo = this.$store.state.userinfo;
			this.creden = this.$store.state.userinfo.creden.split(",");
			this.image_url = this.$store.state.userinfo.image_url.split(",");
			this.head_url = this.userinfo.user_url;
			this.cerad_titles = this.userinfo.creden.split(",");
			this.addPicture = this.userinfo.image_url.split(",");

			console.log(`详情图片获取了吧`);
			console.log(this.addPicture);
			console.log(`详情图片获取了吧`);

			this.educationIndex = this.educationArr.indexOf(this.userinfo.record);
			this.days = this.getTime(this.userinfo.date_time);
		},
		data() {
			return {
				creden: null,
				userinfo: null,
				image_url: null,
				male: 1,
				headPortrait: null,
				addPicture: null,

				days: "请选择",
				educationIndex: 0,
				educationArr: ["请选择", "高中", "大专", "本科", "硕士", "博士"],

				// 用于提交数据给后台的
				certificate: null,
				head_url: null,
				head_urls: null,
				user_name: null,
				pro_title: null,
				back_title: null,
				school: null,
				days: null,
				cerad_title: null,
				cerad_titles: null,
				referral: null,
				image_urls: [],

				//显示和隐藏按钮
				detailsNum: 0,
				bunengai: true,

				// 上传的图片设置
				host: null,
				OSSAccessKeyId: null,
				dir: null,
				policy: null,
				signature: null,
			};
		},
		methods: {
			// 获取oos的地址
			async uploadOosMethos() {
				let fileGetUrl = await this.$myrequest({
					url: `File/get`
				});
				this.host = fileGetUrl.data.data.host;
				this.OSSAccessKeyId = fileGetUrl.data.data.accessid;
				this.dir = fileGetUrl.data.data.dir;
				this.policy = fileGetUrl.data.data.policy;
				this.signature = fileGetUrl.data.data.signature;
			},
			getTime(time) {
				let d = new Date(time * 1000)
				let date = (d.getFullYear()) + "-" +
					(d.getMonth() + 1) + "-" +
					(d.getDate());
				console.log(date)
				return date;
			},
			// 学历选择器
			bindPickerChange(e) {
				if (this.bunengai) return;
				this.educationIndex = e.target.value;
			},
			// 学历选择器

			// 时间选择器
			show() {
				if (this.bunengai) return;
				this.$refs.picker.show()
			},
			confirms(e) {
				this.days = e.detail.value;
			},
			// 时间选择器

			// ==诊所主图==
			async clinicmaingraph() {
				if (this.bunengai) return;
				let self = this;
				uni.chooseImage({
					count: 1,
					success: res => {
						// this.cerad_title = res.tempFilePaths;
						this.creden = res.tempFilePaths;

						let fileName = `${Date.now()}${res.tempFilePaths[0].substr(-4)}`;
						let key = `${self.dir}${fileName}`;
						self.cerad_titles = `${self.host}/${self.dir}${fileName}`;

						uni.uploadFile({
							url: self.host,
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								key,
								policy: self.policy,
								OSSAccessKeyId: this.OSSAccessKeyId,
								signature: this.signature
							},
							success: res => {
								console.log(`诊所主图`);
							}
						});

					}
				});
			},

			// 添加头像
			chooseImages() {
				if (this.bunengai) return;
				let self = this;
				uni.chooseImage({
					count: 1,
					success: res => {
						this.head_url = res.tempFilePaths;

						let fileName = `${Date.now()}${res.tempFilePaths[0].substr(-4)}`;
						let key = `${self.dir}${fileName}`;
						self.head_urls = `${self.host}/${self.dir}${fileName}`;

						uni.uploadFile({
							url: self.host,
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {
								key,
								policy: self.policy,
								OSSAccessKeyId: this.OSSAccessKeyId,
								signature: this.signature
							},
							success: res => {
								console.log(`头部图像`);
							}
						});
					}
				});
			},

			detas() {
				this.detailsNum = 1;
				this.bunengai = false;
			},
			cancel() {
				this.detailsNum = 0;
				this.bunengai = true;
			},

			async examine() {
				console.time(`添加图片了吧`)
				this.detailsNum = 0;

				if (this.head_url === null) {
					uni.showToast({
						title: "头像必填",
						icon: "none"
					})
					return false;
				};
				if (this.userinfo.user_name == "" || this.userinfo.user_name === null) {
					uni.showToast({
						title: "姓名必填",
						icon: "none"
					})
					return false;
				};
				if (this.userinfo.user_sex == "" || this.userinfo.user_sex === null) {
					uni.showToast({
						title: "性别必填",
						icon: "none"
					})
					return false;
				};
				if (this.userinfo.holder == "" || this.userinfo.holder === null) {
					uni.showToast({
						title: "职称必填",
						icon: "none"
					})
					return false;
				};
				// 学历
				if (this.educationIndex == 0) {
					uni.showToast({
						title: "请选择学历",
						icon: "none"
					})
					return false;
				};
				if (this.days == "" || this.days === null || this.days == "请选择" || this.days == 0) {
					uni.showToast({
						title: "出生年月必填",
						icon: "none"
					})
					return false;
				};
				if (this.userinfo.school_title == "" || this.userinfo.school_title === null) {
					uni.showToast({
						title: "毕业院校必填",
						icon: "none"
					})
					return false;
				};
				if (this.userinfo.referral == "" || this.userinfo.referral === null) {
					uni.showToast({
						title: "个人介绍必填",
						icon: "none"
					})
					return false;
				};
				let openids = this.$store.state.openid;
				// 学历下标
				let educationIndexs = this.educationIndex;

				if (this.image_urls.length == 0) {
					this.image_urls = this.image_url;
				};

				let datas = {
					openid: openids,
					// 当头像没有上传时使用默认的头像的url地址了。
					head_url: this.head_urls || this.head_url,
					user_name: this.userinfo.user_name,
					sex: this.userinfo.user_sex,
					pro_title: this.userinfo.holder,
					back_title: this.educationArr[educationIndexs],
					school: this.userinfo.school_title,
					days: this.days,
					cerad_title: this.cerad_titles.toString(),
					referral: this.userinfo.referral,
					image_url: this.image_urls.toString()
				};

				console.log(`datasdatasdatasdatas`);
				console.log(datas);
				console.log(`datasdatasdatasdatas`);

				let res = await this.$myrequest({
					url: `Doctor/editDatum`,
					method: "POST",
					data: datas
				});

				if (res.data.code == 200) {
					this.$store.commit("storePersonalInformation", res.data.data);
					// 成功过了不能再修改了
					this.bunengai = true;
				};
			},
			async certificates() {
				let self = this;
				uni.chooseImage({
					count: 1,
					success: res => {
						this.certificate = res.tempFilePaths;
					}
				});
			},

			async addPictures() {
				if (this.bunengai) return;
				let self = this;
				uni.chooseImage({
					count: 4,
					success: res => {
						this.addPicture = res.tempFilePaths;
						// 上传时将self.image_urls设置为空
						self.image_urls = [];
						for (let [keys, value] of Object.entries(res.tempFilePaths)) {

							let fileName = `${Date.now()+1}${value.substr(-4)}`;
							let key = `${self.dir}${fileName}`;
							self.image_urls.push(`${self.host}/${self.dir}${fileName}`);

							uni.uploadFile({
								url: self.host,
								filePath: value,
								name: 'file',
								formData: {
									key,
									policy: self.policy,
									OSSAccessKeyId: this.OSSAccessKeyId,
									signature: this.signature
								},
								success: res => {
									console.log(`四张头像上传`);
								}
							});
						};
					}
				});
			},

			males() {
				if (this.bunengai) return;
				this.userinfo.user_sex = 1;
			},

			females() {
				if (this.bunengai) return;
				this.userinfo.user_sex = 2;
			},

		}
	}
</script>
<style lang="scss">
	page {
		width: 100%;
		height: 100%;
	}

	.content {
		width: calc(100% - 60rpx);
		height: 100%;
		margin: 0 auto;


		.touxian {
			height: 250rpx;
			position: relative;

			>image {
				border-radius: 50%;
				@include chuizhishuiping;
			}
		}

		.form {

			.userName,
			.region,
			.title,
			.education,
			.university,
			.birth {
				display: flex;
				align-items: center;
				height: 100rpx;
				border-bottom: 1rpx solid #DBDBDB;

				>label {
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 400;
					color: #000000;
					margin-right: 50rpx;
				}

				>input {
					font-size: 30rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #989898;
				}
			}
		}
	}

	.pickers {
		display: flex;
		align-items: center;
		width: 120rpx;
		height: 100%;
		position: relative;
		color: #989898;

		.viewInput {
			padding: 0;
			margin: 0;
		}

		.icons {
			@include chuizhi;
			right: 20rpx;
			color: #989898;
		}
	}

	// 点击按钮
	.joinDetails {
		margin-top: 15rpx;
		margin-bottom: 30rpx;

		width: 100%;
		display: flex;
		justify-content: center;
		align-items: center;

		>text {
			width: 689rpx;
			height: 98rpx;
			background: linear-gradient(90deg, rgba(96, 232, 205, 1), rgba(45, 209, 188, 1));
			box-shadow: 3rpx 4rpx 20rpx 4rpx rgba(15, 170, 150, 0.1);
			border-radius: 49rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #FFFFFF;
		}
	}

	// 底部双按钮
	.dibusuananniu {
		margin-top: 15rpx;
		margin-bottom: 30rpx;
		@include dibusuananniu;
	}

	// 性别小点
	.region {
		padding-right: 20rpx;

		>view {
			display: inline-block;
			@include shuipingchuijuzhong;

			&:first-of-type {
				margin-right: 30rpx;
			}

			text {
				&:first-of-type {
					display: inline-block;
					width: 18rpx;
					height: 18rpx;
					border: 2rpx solid #808080;
					margin-right: 10rpx;
					border-radius: 50%;
				}
			}
		}
	}

	// 性别小点被选中时的状态
	.active {
		background-color: #2DD1BC !important;
		border: 2rpx solid #2DD1BC !important;
	}

	// 出生年月日
	.birth {
		.qingshuru {
			color: #989898;
			margin-right: 10rpx;
		}

		.icon-xiajiantouColor {
			color: #989898;
		}
	}


	// 职业证书
	.clinicmaingraph {
		width: calc(100vw - 60rpx);
		margin: 0 auto;
		border-bottom: 1rpx solid #DBDBDB;

		>view {
			&:first-of-type {
				border-radius: 10rpx;
				height: 100rpx;
				@include chuizhilianduan;
			}
		}
	}

	.personal {
		margin-top: 30rpx;
	}

	// 个人简介
	#clinicProfile {
		width: calc(100vw - 60rpx);
		height: 80rpx;
		flex-wrap: wrap;
		margin: 0 auto;
		padding-left: 10rpx;
		color: #989898;
		border-bottom: 1rpx solid #DBDBDB;
	}


	// 添加图片
	.addPicture {
		margin: 20rpx auto;

		>view {
			&:first-of-type {
				border-radius: 10rpx;
				height: 100rpx;
				padding: 20rpx;
				@include chuizhilianduan;
			}
		}

		.imagess {
			border-radius: 0 0 10rpx 10rpx;
			padding-bottom: 30rpx;
			display: flex;

			image {
				margin-left: 20rpx;
			}

			view {
				display: inline-block;
				width: 150rpx;
				height: 150rpx;
				border: 2rpx dashed #C6C6CA;
				margin-left: 20rpx;
				text-align: center;
				line-height: 150rpx;

				.jiahao {
					font-size: 56rpx;
					color: #BBBCBD;
				}
			}
		}
	}
</style>
